/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.metadata.rtcustomization.common.event;

import com.inspur.edp.metadata.rtcustomization.spi.MimicServiceEventListener;
import io.iec.edp.caf.commons.event.CAFEventArgs;
import io.iec.edp.caf.commons.event.EventManager;
import io.iec.edp.caf.commons.event.IEventListener;

public class MimicServiceEventManager extends EventManager {
    @Override
    public String getEventManagerName() {
        return "MimicServiceEventManager";
    }

    @Override
    public boolean isHandlerListener(IEventListener iEventListener) {
        return iEventListener instanceof MimicServiceEventListener;
    }

    @Override
    public void addListener(IEventListener iEventListener) {
        if (!isHandlerListener(iEventListener)) {
            throw new RuntimeException("指定的监听者" + iEventListener.getClass().getName()
                + "没有实现" + MimicServiceEventListener.class.getName() + "接口");
        }
        MimicServiceEventListener mimicServiceEventListener = (MimicServiceEventListener) iEventListener;
        this.addEventHandler(MimicServiceEventKEY.BEFORE_COPY, mimicServiceEventListener, "fireMetadataBeforeCopyEvent");
        this.addEventHandler(MimicServiceEventKEY.AFTER_COPY, mimicServiceEventListener, "fireMetadataAfterCopyEvent");
        this.addEventHandler(MimicServiceEventKEY.BEFORE_SAVE, mimicServiceEventListener, "fireMetadataBeforeSaveEvent");
        this.addEventHandler(MimicServiceEventKEY.AFTER_SAVE, mimicServiceEventListener, "fireMetadataAfterSaveEvent");
    }

    @Override
    public void removeListener(IEventListener iEventListener) {
        if (!isHandlerListener(iEventListener)) {
            throw new RuntimeException("指定的监听者" + iEventListener.getClass().getName()
                + "没有实现" + MimicServiceEventListener.class.getName() + "接口");
        }
        MimicServiceEventListener mimicServiceEventListener = (MimicServiceEventListener) iEventListener;
        this.removeEventHandler(MimicServiceEventKEY.BEFORE_COPY, mimicServiceEventListener, "fireMetadataBeforeCopyEvent");
        this.removeEventHandler(MimicServiceEventKEY.AFTER_COPY, mimicServiceEventListener, "fireMetadataAfterCopyEvent");
        this.removeEventHandler(MimicServiceEventKEY.BEFORE_SAVE, mimicServiceEventListener, "fireMetadataBeforeSaveEvent");
        this.removeEventHandler(MimicServiceEventKEY.AFTER_SAVE, mimicServiceEventListener, "fireMetadataAfterSaveEvent");
    }

    public void fireMetadataBeforeCopyEvent(CAFEventArgs args) {
        this.fire(MimicServiceEventKEY.BEFORE_COPY, args);
    }

    public void fireMetadataAfterCopyEvent(CAFEventArgs args) {
        this.fire(MimicServiceEventKEY.AFTER_COPY, args);
    }

    public void fireMetadataBeforeSaveEvent(CAFEventArgs args) {
        this.fire(MimicServiceEventKEY.BEFORE_SAVE, args);
    }

    public void fireMetadataAfterSaveEvent(CAFEventArgs args) {
        this.fire(MimicServiceEventKEY.AFTER_SAVE, args);
    }

    enum MimicServiceEventKEY {
        BEFORE_COPY,
        AFTER_COPY,
        BEFORE_SAVE,
        AFTER_SAVE
    }
}
